/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui;

import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.TabSet;
import com.spotimage.catalog.client.event.EventBus;
import com.spotimage.catalog.client.event.SearchResultEvent;
import com.spotimage.catalog.client.event.SelectedSearchResultFootprintEvent;
import com.spotimage.catalog.client.event.SelectedSelectionFootprintEvent;
import com.spotimage.catalog.client.ui.tab.SelectionTab;
import com.spotimage.catalog.client.ui.tab.SearchResultTab;
import com.spotimage.catalog.client.ui.tab.SearchTab;
import com.spotimage.catalog.client.util.Util;

/**
 * User interface
 */
public class Ui implements SearchResultEvent.Handler, 
							SelectedSearchResultFootprintEvent.Handler,
							SelectedSelectionFootprintEvent.Handler {
	
	/** UI header height */
	public static final int HEADER_HEIGHT = 80;
	/** background color */
	public static final String BACKGROUND_COLOR = "#F0F0F0";
	/** border color */
	public static final String BORDER_COLOR = "#C0C3C7";
	
	/** tab set */
	private TabSet tabSet;
	/** search tab */
	private SearchTab searchTab;
	/** result tab */
	private SearchResultTab resultTab;
	/** selection tab */
	private SelectionTab selectionTab;

	/**
	 * Build UI.
	 */
	public Ui() {
		EventBus.getInstance().addHandler(SearchResultEvent.TYPE, this);
		EventBus.getInstance().addHandler(SelectedSearchResultFootprintEvent.TYPE, this);
		EventBus.getInstance().addHandler(SelectedSelectionFootprintEvent.TYPE, this);
		
		VLayout mainLayout = new VLayout();
		mainLayout.setWidth100();
		mainLayout.setHeight100();
		mainLayout.setOverflow(Overflow.HIDDEN);
		
		HLayout headerLayout = new HLayout();
		headerLayout.setWidth100();
		headerLayout.setHeight(HEADER_HEIGHT + "px");
		headerLayout.setAlign(Alignment.RIGHT);
		Label label = new Label("<div style=\"font-size:3em\">Catalog for SPOT images based on GWT</div>");
		label.setWidth100();
		label.setAlign(Alignment.LEFT);
		headerLayout.addMember(label);
		Img logo = new Img(Util.getImageUrl("astrium_3d_xs.png"), 220, 50);  
		headerLayout.addMember(logo);
		mainLayout.addMember(headerLayout);

		MapPanel mapPanel = new MapPanel();
		
		HLayout hLayout = new HLayout();
		hLayout.setWidth100();
		hLayout.setHeight100();
		
		VLayout tabSetLayout = new VLayout();
		tabSetLayout.setBackgroundColor(Ui.BACKGROUND_COLOR);
		tabSetLayout.setShowResizeBar(true);
		tabSetLayout.setWidth("30%");
		tabSetLayout.setHeight100();
		tabSet = new TabSet();
		tabSet.setWidth100();
		tabSet.setHeight100();
		searchTab = new SearchTab(mapPanel);
		tabSet.addTab(searchTab);
		resultTab = new SearchResultTab();
		tabSet.addTab(resultTab);
		selectionTab = new SelectionTab();
		tabSet.addTab(selectionTab);
		tabSetLayout.addMember(tabSet);
		
		hLayout.addMember(tabSetLayout);
		
		hLayout.addMember(mapPanel);
		mapPanel.resizeMap();
		
		mainLayout.addMember(hLayout);
		
		RootPanel.get("mainContainer").add(mainLayout);
	}
	
	/**
	 * Receive {@link SearchResultEvent} and show result tab.
	 */
	//@Override
	public void onSearchResult(SearchResultEvent e) {
		tabSet.selectTab(resultTab);
	}

	/**
	 * Receive {@link SelectedSearchResultFootprintEvent} and show result tab.
	 */
	//@Override
	public void onSelectedSearchResultFootprint(SelectedSearchResultFootprintEvent e) {
		tabSet.selectTab(resultTab);
	}
	
	/**
	 * Receive {@link SelectedSelectionFootprintEvent} and show result tab.
	 */
	//@Override
	public void onSelectedSelectionFootprint(SelectedSelectionFootprintEvent e) {
		tabSet.selectTab(selectionTab);
	}

}
